/**
* Javascipt Library for yui button
* @Ray Li
* dependency
* <script type="text/javascript" src="components/yui/button/button.js"></script>
*/
var TYPE_DEFAULT_YUI_BUTTON					= 1;


function YUIButtons_draw(){
	var bNewLineButtonStart = true, sHTML;
	
	var sStyle = "<style type=\"text/css\">"; 	
	var sScript = "<script type=\"text/javascript\">"; 	
	
	for(var i=0; i<this.arButton.length; i++){
		//IF Seperator
		if(this.arButton[i] != null && this.arButton[i].constructor == YUISeperator){
			bNewLineButtonStart = true;
			if(!bNewLineButtonStart){
				sHTML = "</TR></TABLE>\n" 					
				document.write(sHTML);
			}
			continue;
		}

		if(bNewLineButtonStart){
			var sHTML = "<TABLE border=\"0\" cellpadding=\"0\" cellspacing=\""+this.cellSpacing+"\">\n" 
				+"<TR>\n";
			document.write(sHTML);
			bNewLineButtonStart = false;
		}

		if(this.cellSpacing==0){
			document.write("<TD width=10>&nbsp;&nbsp;</TD>");
		}

		sHTML = "<TD title=\""+(this.arButton[i].sTitle)+"\">\n" 					
		document.write(sHTML);
		
		if(this.arButton[i] != null && this.arButton[i].constructor == YUIButton){
			this.arButton[i].draw();
		}else{
			document.write("["+(i+1)+"]Button invalid!");
		}

		sHTML = "</TD>\n" 					

		if(this.cellSpacing==0){
			document.write("<TD width=10>&nbsp;&nbsp;</TD>");
		}
        
        //write buttons html
		document.write(sHTML);
		
		//construct css style
			
		sStyle += ".yui-button#" + this.arButton[i].sBtnid + " button {";
        sStyle += "text-align: left;";
        sStyle += "padding-left: 1.7em;";
        sStyle += "background: url(" + this.arButton[i].sButtonImgSrc + ") 5% 50% no-repeat;";
		sStyle += "width: 82px;";
	    sStyle += "height: 21px;} ";
	    
	    //construct java script
		sScript += "var o" + this.arButton[i].sBtnid + " = new YAHOO.widget.Button(\""+ this.arButton[i].sBtnid + "\"";
		if(this.arButton[i].sClickFunc){
		    sScript += ", { onclick: { fn: " + this.arButton[i].sClickFunc + " } }); ";
		}else{
		    sScript += "); ";
		}
		
	}

	if(!bNewLineButtonStart){
		sHTML = "</TR></TABLE>\n" 					
		document.write(sHTML);
	}
	
	sStyle += "</style> ";
	sScript += "</script> ";
	document.write(sStyle);
	document.write(sScript);
	
}

function YUIButton_draw(){		
	document.write(this.toHTML());
}

function YUIButton_toHTML(){		
	var sHTML ;
	switch(this.nType){
		case TYPE_DEFAULT_YUI_BUTTON:
		default:
			if (this.sStyleName.indexOf("_disable")>0)
			{ // disabled button
				sHTML = ""
				+"<TABLE disabled=true class=\""+this.sStyleName+"\">" 
				+"	<TR>" ;
			} else { // enabled button
				sHTML = ""
				+"<TABLE class=\""+this.sStyleName+"\">" 
				+"	<TR onclick=\""+this.sClickFunc+"\">" 
			} // end of if...else...

			sHTML = sHTML + "		<TD><IMG src=\""+this.sButtonImgSrc+"\"></TD>" 
			+"		<TD nowrap>"+this.sText+"</TD>" 
			+"	</TR>" 
			+"</TABLE>";
			
			sHTML = "<input type=\"button\" id=\"" + this.sBtnid + "\" value=\"" + this.sText + "\">";

			break;		
	} // end of switch...case...

	return sHTML;
}


function YUISeperator(){
}


function YUIButton(_sBtnid, _sText, _sClickFunc, _sButtonImgSrc, _sToolTip,  _sStyleName){
    this.sBtnid = _sBtnid || "button";
	this.sText			= _sText		|| "Default";
	this.sClickFunc		= _sClickFunc	|| "";
	this.sButtonImgSrc	= _sButtonImgSrc|| "../../../../images/button_default.gif";
	this.sTitle			= _sToolTip		|| this.sText;
	this.sStyleName		= _sStyleName	|| "bt_table";
	this.sLinkHref		;
	this.sTarget		;	
	this.nType			= TYPE_DEFAULT_YUI_BUTTON;

	this.draw			= YUIButton_draw;
	
	this.toHTML			= YUIButton_toHTML;

}


function YUIButton_gotoLink(_elTR){
	var arA = _elTR.getElementsByTagName("A");
	if(arA == null || arA.length<=0){
		alert("Not Deifine!");
		return;
	}
	arA[0].click();
}



function YUIButtons(_sBtnsid, _sStyleName, _nType){
    this.sBtnsid = _sBtnsid || "button";
	this.nType		 =	_nType		||	TYPE_DEFAULT_YUI_BUTTON;
	this.cellSpacing = "3";	
	this.styleName	 = _sStyleName	||	"bt_table";	
	this.arButton = [];
	

	//Define Methods
	this.addButton		= function(_sText, _sClickFunc, _sButtonImgSrc, _sToolTip, _sStyleName){
		var oButton = new YUIButton(this.sBtnsid + this.arButton.length, _sText, _sClickFunc, _sButtonImgSrc, _sToolTip, _sStyleName || this.styleName);
		oButton.nType = this.nType;
		this.arButton[this.arButton.length] = oButton;
	}

	this.addDisableButton= function(_sText, _sClickFunc, _sButtonImgSrc, _sToolTip){
		this.addButton(_sText, _sClickFunc, _sButtonImgSrc, _sToolTip, "bt_table_disable");		
	}

	this.addLinkButton	= function(_sText, _sButtonImgSrc, _sLink, _sTarget, _sClickFunc,  _sToolTip, _sStyleName){
		var oButton = new YUIButton(_sText, _sClickFunc, _sButtonImgSrc, _sToolTip, _sStyleName || this.styleName);

		oButton.nType	= TYPE_LINK_BUTTON;
		oButton.sLink	= _sLink	|| "";
		oButton.sTarget	= _sTarget	|| "";

		this.arButton[this.arButton.length] = oButton;
	}

	this.addSeperator	= function(){
		this.arButton[this.arButton.length] = new YUISeperator();
	}

	this.draw				= YUIButtons_draw;	
}
